package com.zhou.review3.controller;

import com.zhou.review3.entity.Goods;
import com.zhou.review3.serviceImpl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/Goods")
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsServiceImpl;
    //查看内容
    @RequestMapping("list.do")
    public String list(Model model){
        List<Goods> goods=goodsServiceImpl.findAllGoods();
        model.addAttribute("goods",goods);
        return "listGoods";
    }
    //删除
    @RequestMapping("delete.do")
    public String delete(int id,Model model){
        goodsServiceImpl.removeGoods(id);
        return list(model);
    }

    //增加
    @RequestMapping("add.do")
    public String add(Goods goods,Model model){
        goodsServiceImpl.addGoods(goods);
        return list(model);
    }
    //toAdd.do,准备增加
    @RequestMapping("toAdd.do")
    public String toAdd(Model model){
        List<Goods> goods=goodsServiceImpl.findAllGoods();
        model.addAttribute("goods",goods);
        return "addGoods";
    }
    //更新
    @RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model){
        Goods oldGoods=goodsServiceImpl.findGoods(id);
        model.addAttribute("oldGoods",oldGoods);
        return "updateGoods";
    }
    //update,更新数据到数据库，回到listStudents.html
    @RequestMapping("update.do")
    public String update(Goods newGoods,Model model){
        goodsServiceImpl.modifyGoods (newGoods);
        return list(model);
    }

}
